*******************************************************************************
*set working directory
global folder ".."


cd $folder\temp
/*

/* treasury return */
use "$folder/data/security_level_analysis/CRSP_treasury_2019Q4_2020Q2.dta", clear

rename *, lower
rename tcusip cusip
keep if (caldt)>=td(01jan2020) & (caldt)<=td(30apr2020) // flows are only available for this quarter

g yield = tdyld*365*100
g idn = kytreasno
g date = caldt
format date %td
tsset idn date

tsset idn date
*bysort idn: g return = (tdnomprc/tdnomprc[_n-1]-1)*100
bysort idn: g return = -(tdyld-tdyld[_n-1])*tdduratn*100
sum ret, d


isid cusip date

keep cusip idn caldt date yield return 

winsor2 return, cut(1 99) suffix(_win)
save treasury_ret, replace


/* corp return */
use "$folder/data/security_level_analysis/TRACE_BTDS_trades_2019Q4_2020Q2.dta", clear
sort bond_sym_id trd_exctn_dt trd_exctn_tm
bysort bond_sym_id trd_exctn_dt: keep if _n==_N  

rename   trd_exctn_dt date
rename cusip_id cusip
g caldt = date

format date %td
keep if date>=td(1jan2020)& date<=td(30apr2020)

encode bond_sym_id, g(idn)
g yield = yld_pt

tsset idn date
tsfill, full
bysort id: carryforward cusip  yield rptd_pr, replace

bysort idn: g return = (rptd_pr/rptd_pr[_n-1]-1)*100
sum ret, d

drop if missing(cusip)
duplicates drop cusip date, force // 49 obs
isid cusip date
g dow=dow(date)
g zero_return=(return==0)
tab dow zero // 30 of bond-day obs have zero returns 
drop if dow==0 | dow==6
keep cusip idn date yield return 
winsor2 return, cut(1 99) suffix(_win)
save "corp_ret", replace


/* append bond returns, unique cusip list to reduce size*/
use "treasury_ret", clear
g treasury_dummy=1
append using "corp_ret"
replace treasury_dummy=0 if treasury_dummy==.

egen id=group(treasury_dummy idn)
drop idn
g cusip_8 = substr(cusip, 1, 8)
drop cusip
rename cusip_8 cusip
save "bond_ret", replace
keep cusip 
duplicates drop
save "unique_cusip_with_ret", replace


/* weight for imputed flows */
use "$folder/data\security_level_analysis/holdings_2019Q4_2020Q2.dta" if report_dt==td(31dec2019), clear
replace nbr_shares=0 if nbr_shares<0
bysort cusip: egen sum_nbr_shares=sum(nbr_shares)
g weight=nbr_shares/sum_nbr_shares
drop if missing(cusip)|missing(crsp_portno)|missing(weight)
duplicates drop  crsp_portno cusip, force
keep crsp_portno cusip weight 
replace weight = 0 if weight<0
save nbr_share_weight, replace



/* imputed flows */
use "daily_flow_assets", clear
merge m:1 SecId using "Link_SecId_crsp_fundno"
keep if _m==3
drop _m

sort SecId date
*bysort SecId: carryforward assets, replace // some dates miss flows but have assets

collapse (sum) flow assets, by(crsp_portno date)

g portno_flow_pct = flow/assets

sum portno_flow_pct, d
winsor2 portno_flow_pct, cut(1 99) replace
keep crsp_portno date portno_flow_pct
duplicates drop
save "portno_flow_pct.dta", replace


use "nbr_share_weight", clear
merge m:1 cusip using "unique_cusip_with_ret"
keep if _m==3 
drop _m

/* weight for imputed flows */
joinby crsp_portno using "portno_flow_pct.dta"
order crsp_portno cusip date 
sort crsp_portno cusip date 
save temp, replace

use temp, clear
merge m:1 crsp_portno using "fund_obj"
keep if _m==3
drop _m
drop if etf==1
*drop if index==1
replace weight = 0 if weight<0
collapse (mean) portno_flow_pct [iw=weight], by(cusip date)
save "imputed_flow", replace


use temp, clear
merge m:1 crsp_portno using "fund_obj"
keep if _m==3
drop _m
keep if etf==1
*drop if index==1
replace weight = 0 if weight<0
collapse (mean) portno_flow_pct_etf=portno_flow_pct [iw=weight], by(cusip date)
save "imputed_flow_etf", replace


/* imputed liquidation*/
use "temp", clear
merge m:1 crsp_portno cusip using "portno_cusip_rank_2019"
keep if _m==3
drop _m
merge m:1 crsp_portno using "fund_obj"
keep if _m==3
drop _m
*keep if sum_perc>=50
drop if etf==1

replace weight = 0 if weight<0
g nrank = (1-rank)
g liquidation = -100*portno_flow_pct*(0.208+0.386*nrank) if index==0
replace liquidation = -100*portno_flow_pct*(0.224+0.082*nrank) if index==1 & etf==0
*replace liquidation = -100*portno_flow_pct*(1.141+0.248*nrank) if  etf==1


collapse (mean) liquidation  [iw=weight], by(cusip date)
save "imputed_liquidation", replace


/* imputed liquidation*/
use "temp", clear
merge m:1 crsp_portno cusip using "portno_cusip_rank_2019"
keep if _m==3
drop _m
merge m:1 crsp_portno using "fund_obj"
keep if _m==3
drop _m
keep if etf==1

replace weight = 0 if weight<0
g nrank = (1-rank)
g liquidation_etf = -100*portno_flow_pct*(0.208+0.386*nrank) if index==0
replace liquidation = -100*portno_flow_pct*(0.224+0.082*nrank) if index==1 & etf==0
replace liquidation = -100*portno_flow_pct*(1.141+0.248*nrank) if  etf==1


collapse (mean) liquidation_etf=liquidation  [iw=weight], by(cusip date)
save "imputed_liquidation_etf", replace


/* rank_2019 */
use "rank", clear
keep if caldt ==td(31dec2019)
drop caldt
save "rank_2019", replace


/* treasury type as of 2019Q4*/
use "treasury_type", clear
keep if tm ==tm(2019m12)
keep cusip on_the_run maturity_at_issue
duplicates drop
save "treasury_type_2019m12", replace

/* average liquidity ranking  */
use "nbr_share_weight", clear
merge m:1 cusip  using "Mergent_issues_ratings"
keep if _m==3
drop _m
capture drop maturity_year
g maturity_year = round((maturity - td(31dec2019))/365)
merge m:1 cusip   using "treasury_type_2019m12"
drop if _m==2
drop _m

g liquidity_group = rating_num
/*
replace liquidity_group=11 if (substr(bond_type,1,1)=="U"|substr(bond_type,1,1)=="O") & maturity_year>=10
replace liquidity_group=12 if (substr(bond_type,1,1)=="U"|substr(bond_type,1,1)=="O") & maturity_year>=1 & maturity_year<10
replace liquidity_group=13 if (substr(bond_type,1,1)=="U"|substr(bond_type,1,1)=="O") & maturity_year<1
*/
replace liquidity_group=11 if on_the_run==0
replace liquidity_group=12 if on_the_run==1

drop if missing(liquidity_group)

merge m:1 crsp_portno liquidity_group using "rank_2019"
keep if _m==3
drop _m
save "portno_cusip_rank_2019", replace

/* average liquidity ranking  */
use "portno_cusip_rank_2019", clear
*keep if sum_perc>=50
collapse (mean) rank [iw=weight], by(cusip)
save "cusip_rank_2019", replace

/* average liquidity ranking (index fund adjusted)  */
use "portno_cusip_rank_2019", clear
merge m:1 crsp_portno using "fund_obj"
keep if _m==3
drop _m
*keep if sum_perc>=50
drop if etf==1
sum rank if index==1
 *sum rank 
replace rank=`r(mean)' if index==1
collapse (mean) rank_index_adj=rank [iw=weight], by(cusip)
save "cusip_rank_2019_index_adj", replace


/* ratio of mutual fund holding */
use "$folder/data\security_level_analysis/holdings_2019Q4_2020Q2.dta" ///
if report_dt==td(31dec2019), clear
merge m:1 crsp_portno using "fund_obj"
keep if _m==3
drop _m
drop if etf==1

collapse (sum) nbr_shares, by(cusip )
merge 1:1 cusip  using "Mergent_issues_ratings_outstanding"
drop if _m==2
drop _m
g mf_share = nbr_shares/(amount_outstanding*1000) //The table, AMOUNT_OUTSTANDING, contains the current dollaramount that remains
merge 1:1 cusip  using "treasury_outstanding"
replace mf_share = nbr_shares/(tdtotout*10^6) if _m==3 //expressed in millions of dollars.
g ratio =amount_outstanding/ tdtotout
sum ratio, d // amount_outstanding in Mergent is in thousands 
drop ratio
tab bond_type if _m==3, sum( mf_share)
drop if _m==2
drop _m

sum mf_share, d
replace mf_share=0 if mf_share<0
replace mf_share=1 if mf_share>=1 & mf_share!=.
sum mf_share, d
keep cusip mf_share 
save "mf_share", replace

/* ratio of etf  holding */
use "$folder/data\security_level_analysis/holdings_2019Q4_2020Q2.dta" ///
if report_dt==td(31dec2019), clear
merge m:1 crsp_portno using "fund_obj"
keep if _m==3
drop _m
keep if etf==1

collapse (sum) nbr_shares, by(cusip )
merge 1:1 cusip  using "Mergent_issues_ratings_outstanding"
drop if _m==2
drop _m
g etf_share = nbr_shares/amount_outstanding/1000
merge 1:1 cusip  using "treasury_outstanding"
replace etf_share = nbr_shares/tdtotout/10^6 if _m==3
tab bond_type if _m==3, sum( etf_share)
drop if _m==2
drop _m

sum etf_share, d
replace etf_share=0 if etf_share<0
replace etf_share=1 if etf_share>=1 & etf_share!=.
sum etf_share, d
keep cusip etf_share 
save "etf_share", replace

/* merge flow with return */
use  "imputed_flow", clear

merge 1:1 cusip date using "imputed_liquidation"
drop if _m==2
drop _m

merge 1:1 cusip date using "imputed_flow_etf"
drop if _m==2
drop _m

merge 1:1 cusip date using "imputed_liquidation_etf"
drop if _m==2
drop _m

merge 1:1 cusip date using "bond_ret"
g dow=dow(date)
tab dow _m 
tab dow _m  if treas==1
keep if _m==3
drop _m

merge m:1 cusip  using "cusip_rank_2019"
keep if _m==3
drop _m
merge m:1 cusip  using "cusip_rank_2019_index_adj"
keep if _m==3
drop _m

*merge m:1 cusip  using "Mergent_issues_ratings"
merge m:1 cusip using "Mergent_issues_ratings_outstanding.dta"
keep if _m==3
drop _m


merge 1:1 cusip date using "imputed_liquidation"
drop if _m==2
drop _m

merge m:1 cusip  using "mf_share"
drop if _m==2
drop _m

merge m:1 cusip  using "etf_share"
drop if _m==2
drop _m

g tm = mofd(caldt)
format tm %tm
merge m:1 cusip tm  using "treasury_type"
drop if _m==2
drop _m

g maturity_year = round((maturity-td(12dec2019))/365)
*drop if maturity_year<10 & treasury_dummy==1

g outflow	= portno_flow_pct*(-100)
g outflow_etf	= portno_flow_pct_etf*(-100)

g nrank = (1-rank)
g nrank_adj = (1-rank_index_adj)

g cusip_6 = substr(cusip,1,6)

egen issuer_time_fe=group(cusip_6 date)

sum nrank, d
g nrank_dummy=(nrank>`r(p50)') 

sum nrank_adj , d
g nrank_adj_dummy=(nrank_adj>`r(p50)')

egen maturity_time = group(maturity_year date)
egen rating_time = group(rating_num date)
egen rating_maturity_time = group(rating_num maturity date)
save return_security_day_level, replace

bb
*/

*********************************************************************
* regression
*********************************************************************


use "$folder\data\return_security_day_level", clear

replace return_win = return_win*100 // change to basis points 
tsset id date

label var outflow "Imputed outflow"
label var outflow_etf "ETF outflow"
label var liquidation "Liquidation-adjusted outflow"
label var liquidation_etf "ETF liquidation"



encode cusip_6, g(cusip_6_n)
egen issuer_time = group(cusip_6 date)
egen maturity_at_issue_time = group(maturity_at_issue date)

keep if date>=td(01jan2020)&date<=td(31mar2020)

replace yield = yield*100
replace mf_share=mf_share*100
winsor2 yield, cut(1 99) replace
g crisis = (date>=td(1mar2020)&date<=td(10apr2020))
replace mf_share=0 if mf_share==.
replace etf_share=0 if etf_share==.
foreach var of varlist crisis  outflow liquidation {
g `var'_mf_share = `var'*mf_share
}


label var crisis_mf_share "Crisis*Mutual fund share"

 * Treasury (imputed outflow)
 set more off
eststo clear
local contr 
local y return_win 
local x  outflow outflow_etf
local cluster date
local fe maturity_time id
eststo:reghdfe `y'  `x' `contr' if  Tbill==1,  ab(`fe' ) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr' if on_the_run==1 & Tnote==1,  ab(`fe') cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
eststo:reghdfe `y'  `x' `contr' if on_the_run==0 & Tnote==1,  ab(`fe') cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
eststo:reghdfe `y'  `x' `contr' if on_the_run==1 & Tbond==1,  ab(`fe') cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
eststo:reghdfe `y'  `x' `contr' if on_the_run==0 & Tbond==1,  ab(`fe') cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
esttab
esttab using  "$folder\output\return_outflow_treasury_maturity_time_fe.tex", ///
 replace b(3) se(3) star(* 0.10 ** 0.05 *** 0.01  )   ///
 mtitles("\shortstack{T-bill\\all}" "\shortstack{T-note\\on-the-run}" "\shortstack{T-note\\off-the-run}" ///
 "\shortstack{T-bond\\on-the-run}"  "\shortstack{T-bond\\off-the-run}") ///
ar2  br  label nocon  nonotes  s(control FE1 FE2  N r2_a, ///
label("Control" "Maturity-Time F.E."   "Bond F.E."  "Observations" "Adj. R-squared") ///
 fmt(0 0  0 %9.0fc 3 )) width(\hsize) drop(outflow_etf)

 
 
 * test wether interaction term is significant or not
set more off
eststo clear
local contr 
local y return_win 
local x   outflow_etf
local cluster date
local fe maturity_time id
reghdfe `y' outflow i.on_the_run#c.outflow `x' `contr' if  Tbond==1,  ab(`fe') cluster(`cluster')
reghdfe `y' liquidation i.on_the_run#c.liquidation `x' `contr' if  Tbond==1,  ab(`fe') cluster(`cluster')

  
 * Treasury (imputed liquidation)
set more off
eststo clear
local contr 
local y return_win 
local x  liquidation outflow_etf
local cluster date
local fe maturity_time id
eststo:reghdfe `y'  `x' `contr' if  Tbill==1,  ab(`fe' ) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr' if on_the_run==1 & Tnote==1,  ab(`fe') cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
eststo:reghdfe `y'  `x' `contr' if on_the_run==0 & Tnote==1,  ab(`fe') cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
eststo:reghdfe `y'  `x' `contr' if on_the_run==1 & Tbond==1,  ab(`fe') cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
eststo:reghdfe `y'  `x' `contr' if on_the_run==0 & Tbond==1,  ab(`fe') cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"

esttab
esttab using  "$folder\output\return_liquidation_treasury_maturity_time_fe.tex", ///
 replace b(3) se(3) star(* 0.10 ** 0.05 *** 0.01  )   ///
 mtitles("\shortstack{T-bill\\all}"  "\shortstack{T-note\\on-the-run}" "\shortstack{T-note\\off-the-run}" ///
 "\shortstack{T-bond\\on-the-run}"  "\shortstack{T-bond\\off-the-run}") ///
ar2  br  label nocon  nonotes  s(control FE1 FE2  N r2_a, ///
label("Control" "Maturity-Time F.E."   "Bond F.E."  "Observations" "Adj. R-squared") ///
 fmt(0 0  0 %9.0fc 3 )) width(\hsize) drop(outflow_etf)


 
 * Corp
 set more off
eststo clear
local contr 
local y return_win 
local x   liquidation outflow_etf 
local cluster 
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0 ,  ab( rating_time maturity_time ) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr'   if treasury_dummy==0,  ab( rating_time  maturity_tim cusip_6_n) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "Yes"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0,  ab( rating_time maturity_time id) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "Yes"
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0,  ab(  rating_time maturity_time issuer_time id) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "Yes"
estadd local FE4 "No"
estadd local FE5 "Yes"
esttab
esttab using  "$folder\output\return_liquidation_corp.tex", ///
 replace b(3) se(3) star(* 0.10 ** 0.05 *** 0.01  )   ///
mtitles("Return" "Return" "Return" "Return") ///
ar2  br  label nocon  nonotes  s(control FE1 FE2 FE3 FE4 FE5  N r2_a, ///
label("Control" "Rating-Time F.E."  "Maturity-Time F.E." "Issuer-Time F.E."  "Issuer F.E."  "Bond F.E."  "Observations" "Adj. R-squared") ///
 fmt(0 0  0  0 0 0 %9.0fc 3 )) width(\hsize) drop(outflow_etf)

 set more off
eststo clear
local contr 
local y return_win 
local x   outflow outflow_etf 
local cluster 
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0 ,  ab( rating_time maturity_time ) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr'   if treasury_dummy==0,  ab( rating_time  maturity_tim cusip_6_n) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "Yes"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0,  ab( rating_time maturity_time id) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "Yes"
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0,  ab(  rating_time maturity_time issuer_time id) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "Yes"
estadd local FE4 "No"
estadd local FE5 "Yes"
esttab
esttab using  "$folder\output\return_outflow_corp.tex", ///
 replace b(3) se(3) star(* 0.10 ** 0.05 *** 0.01  )   ///
mtitles("Return" "Return" "Return" "Return") ///
ar2  br  label nocon  nonotes  s(control FE1 FE2 FE3 FE4 FE5  N r2_a, ///
label("Control" "Rating-Time F.E."  "Maturity-Time F.E." "Issuer-Time F.E."  "Issuer F.E."  "Bond F.E."  "Observations" "Adj. R-squared") ///
 fmt(0 0  0  0 0 0 %9.0fc 3 )) width(\hsize) drop(outflow_etf)


 ******************************************
* Corp: seperate on the run and off the run
******************************************
g bond_age = round((td(31dec2019)-offering_date)/365,1)

bysort cusip_6 date: g N=_N
bysort  cusip_6: egen max_N = max(N)
tab max_N
g multiple_issues = (max_N>1)

 set more off
eststo clear
local contr 
local y return_win 
local x  liquidation outflow_etf 
local cluster 
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0 & bond_age<=1,  ab( rating_time maturity_time ) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr'   if treasury_dummy==0 & bond_age<=1,  ab( rating_time  maturity_tim cusip_6_n) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "Yes"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0 & bond_age>1,  ab( rating_time maturity_time ) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr'   if treasury_dummy==0 & bond_age>1,  ab( rating_time  maturity_tim cusip_6_n) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "Yes"
estadd local FE5 "No"
esttab
esttab using  "$folder\output\return_liquidation_on_off.tex", ///
 replace b(3) se(3) star(* 0.10 ** 0.05 *** 0.01  )   ///
mtitles("Age$\leq$1" "Age$\leq$1" "Age$>$1" "Age$>$1") ///
ar2  br  label nocon  nonotes  s(control FE1 FE2 FE4   N r2_a, ///
label("Control" "Rating-Time F.E."  "Maturity-Time F.E."   "Issuer F.E."    "Observations" "Adj. R-squared") ///
 fmt(0 0  0  0 %9.0fc 3 )) width(\hsize) drop(outflow_etf)



  set more off
eststo clear
local contr 
local y return_win 
local x  outflow outflow_etf 
local cluster 
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0 & bond_age<=1,  ab( rating_time maturity_time ) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr'   if treasury_dummy==0 & bond_age<=1,  ab( rating_time  maturity_tim cusip_6_n) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "Yes"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0 & bond_age>1,  ab( rating_time maturity_time ) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr'   if treasury_dummy==0 & bond_age>1,  ab( rating_time  maturity_tim cusip_6_n) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "Yes"
estadd local FE5 "No"
esttab
esttab using  "$folder\output\return_outflow_on_off.tex", ///
 replace b(3) se(3) star(* 0.10 ** 0.05 *** 0.01  )   ///
mtitles("Age$\leq$1" "Age$\leq$1" "Age$>$1" "Age$>$1") ///
ar2  br  label nocon  nonotes  s(control FE1 FE2 FE4   N r2_a, ///
label("Control" "Rating-Time F.E."  "Maturity-Time F.E."   "Issuer F.E."    "Observations" "Adj. R-squared") ///
 fmt(0 0  0  0 %9.0fc 3 )) width(\hsize) drop(outflow_etf)




******************************************
* Corp: seperate IG and HY
******************************************
 set more off
eststo clear
local contr 
local y return_win 
local x  liquidation outflow_etf 
local cluster 
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0 & rating_num>=1,  ab( rating_time maturity_time ) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr'   if treasury_dummy==0 & rating_num>=1,  ab( rating_time  maturity_tim cusip_6_n) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "Yes"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0 & rating_num<1,  ab( rating_time maturity_time ) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr'   if treasury_dummy==0 & rating_num<1,  ab( rating_time  maturity_tim cusip_6_n) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "Yes"
estadd local FE5 "No"
esttab
esttab using  "$folder\output\return_liquidation_IG_HY.tex", ///
 replace b(3) se(3) star(* 0.10 ** 0.05 *** 0.01  )   ///
mtitles("IG" "IG" "HY" "HY") ///
ar2  br  label nocon  nonotes  s(control FE1 FE2 FE4   N r2_a, ///
label("Control" "Rating-Time F.E."  "Maturity-Time F.E."   "Issuer F.E."    "Observations" "Adj. R-squared") ///
 fmt(0 0  0  0 %9.0fc 3 )) width(\hsize) drop(outflow_etf)



  set more off
eststo clear
local contr 
local y return_win 
local x  outflow outflow_etf 
local cluster 
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0 & rating_num>=1,  ab( rating_time maturity_time ) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr'   if treasury_dummy==0 & rating_num>=1,  ab( rating_time  maturity_tim cusip_6_n) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "Yes"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0 & rating_num<1,  ab( rating_time maturity_time ) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr'   if treasury_dummy==0 & rating_num<1,  ab( rating_time  maturity_tim cusip_6_n) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "Yes"
estadd local FE5 "No"
esttab
esttab using  "$folder\output\return_outflow_IG_HY.tex", ///
 replace b(3) se(3) star(* 0.10 ** 0.05 *** 0.01  )   ///
mtitles("IG" "IG" "HY" "HY") ///
ar2  br  label nocon  nonotes  s(control FE1 FE2 FE4   N r2_a, ///
label("Control" "Rating-Time F.E."  "Maturity-Time F.E."   "Issuer F.E."    "Observations" "Adj. R-squared") ///
 fmt(0 0  0  0 %9.0fc 3 )) width(\hsize) drop(outflow_etf)

 
 
 * Internet appendix: without controlling for ETFs
 drop if missing(outflow_etf)
  * Treasury (imputed outflow)
 set more off
eststo clear
local contr 
local y return_win 
local x  outflow 
local cluster date
local fe maturity_time id
eststo:reghdfe `y'  `x' `contr' if  Tbill==1,  ab(`fe' ) cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr' if on_the_run==1 & Tnote==1,  ab(`fe') cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
eststo:reghdfe `y'  `x' `contr' if on_the_run==0 & Tnote==1,  ab(`fe') cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
eststo:reghdfe `y'  `x' `contr' if on_the_run==1 & Tbond==1,  ab(`fe') cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
eststo:reghdfe `y'  `x' `contr' if on_the_run==0 & Tbond==1,  ab(`fe') cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
esttab
esttab using  "$folder\output\return_outflow_treasury_maturity_time_fe_IA.tex", ///
 replace b(3) se(3) star(* 0.10 ** 0.05 *** 0.01  )   ///
 mtitles("\shortstack{T-bill\\all}" "\shortstack{T-note\\on-the-run}" "\shortstack{T-note\\off-the-run}" ///
 "\shortstack{T-bond\\on-the-run}"  "\shortstack{T-bond\\off-the-run}") ///
ar2  br  label nocon  nonotes  s(control FE1 FE2  N r2_a, ///
label("Control" "Maturity-Time F.E."   "Bond F.E."  "Observations" "Adj. R-squared") ///
 fmt(0 0  0 %9.0fc 3 )) width(\hsize)

  
  
  
 * Treasury (imputed liquidation)
set more off
eststo clear
local contr 
local y return_win 
local x  liquidation 
local cluster date
local fe maturity_time id
eststo:reghdfe `y'  `x' `contr' if  Tbill==1,  ab(`fe' ) cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr' if on_the_run==1 & Tnote==1,  ab(`fe') cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
eststo:reghdfe `y'  `x' `contr' if on_the_run==0 & Tnote==1,  ab(`fe') cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
eststo:reghdfe `y'  `x' `contr' if on_the_run==1 & Tbond==1,  ab(`fe') cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
eststo:reghdfe `y'  `x' `contr' if on_the_run==0 & Tbond==1,  ab(`fe') cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"

esttab
esttab using  "$folder\output\return_liquidation_treasury_maturity_time_fe_IA.tex", ///
 replace b(3) se(3) star(* 0.10 ** 0.05 *** 0.01  )   ///
 mtitles("\shortstack{T-bill\\all}"  "\shortstack{T-note\\on-the-run}" "\shortstack{T-note\\off-the-run}" ///
 "\shortstack{T-bond\\on-the-run}"  "\shortstack{T-bond\\off-the-run}") ///
ar2  br  label nocon  nonotes  s(control FE1 FE2  N r2_a, ///
label("Control" "Maturity-Time F.E."   "Bond F.E."  "Observations" "Adj. R-squared") ///
 fmt(0 0  0 %9.0fc 3 )) width(\hsize) 


 
 * Corp
 set more off
eststo clear
local contr 
local y return_win 
local x   liquidation  
local cluster 
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0 ,  ab( rating_time maturity_time ) cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr'   if treasury_dummy==0,  ab( rating_time  maturity_tim cusip_6_n) cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "Yes"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0,  ab( rating_time maturity_time id) cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "Yes"
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0,  ab(  rating_time maturity_time issuer_time id) cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "Yes"
estadd local FE4 "No"
estadd local FE5 "Yes"
esttab
esttab using  "$folder\output\return_liquidation_corp_IA.tex", ///
 replace b(3) se(3) star(* 0.10 ** 0.05 *** 0.01  )   ///
mtitles("Return" "Return" "Return" "Return") ///
ar2  br  label nocon  nonotes  s(control FE1 FE2 FE3 FE4 FE5  N r2_a, ///
label("Control" "Rating-Time F.E."  "Maturity-Time F.E." "Issuer-Time F.E."  "Issuer F.E."  "Bond F.E."  "Observations" "Adj. R-squared") ///
 fmt(0 0  0  0 0 0 %9.0fc 3 )) width(\hsize)
 
 
 set more off
eststo clear
local contr 
local y return_win 
local x   outflow  
local cluster 
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0 ,  ab( rating_time maturity_time ) cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr'   if treasury_dummy==0,  ab( rating_time  maturity_tim cusip_6_n) cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "Yes"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0,  ab( rating_time maturity_time id) cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "Yes"
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0,  ab(  rating_time maturity_time issuer_time id) cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "Yes"
estadd local FE4 "No"
estadd local FE5 "Yes"
esttab
esttab using  "$folder\output\return_outflow_corp_IA.tex", ///
 replace b(3) se(3) star(* 0.10 ** 0.05 *** 0.01  )   ///
mtitles("Return" "Return" "Return" "Return") ///
ar2  br  label nocon  nonotes  s(control FE1 FE2 FE3 FE4 FE5  N r2_a, ///
label("Control" "Rating-Time F.E."  "Maturity-Time F.E." "Issuer-Time F.E."  "Issuer F.E."  "Bond F.E."  "Observations" "Adj. R-squared") ///
 fmt(0 0  0  0 0 0 %9.0fc 3 )) width(\hsize) 

 
******************************************************
* control for zero trading for return flow sensitivity
*********************************************************************

use $\folder\data\return_security_day_level, clear

replace return_win = return_win*100 // change to basis points 
tsset id date

label var outflow "Imputed outflow"
label var outflow_etf "ETF outflow"
label var liquidation "Liquidation-adjusted outflow"
label var liquidation_etf "ETF liquidation"



encode cusip_6, g(cusip_6_n)
egen issuer_time = group(cusip_6 date)
egen maturity_at_issue_time = group(maturity_at_issue date)

keep if date>=td(01jan2020)&date<=td(31mar2020)

replace yield = yield*100
replace mf_share=mf_share*100
winsor2 yield, cut(1 99) replace
g crisis = (date>=td(1mar2020)&date<=td(10apr2020))
replace mf_share=0 if mf_share==.
replace etf_share=0 if etf_share==.
foreach var of varlist crisis  outflow liquidation {
g `var'_mf_share = `var'*mf_share
}


label var crisis_mf_share "Crisis*Mutual fund share"

 drop tm
g tm = mofd(date)
format tm %tm
merge m:1 cusip tm using "liquidity_2020"
drop if _m==2
drop _m
 egen zero_trading_4group = xtile(zero_trading),  nq(4)
 
 * Corp
 set more off
eststo clear
local contr 
local y return_win 
local x   liquidation outflow_etf  zero_trading
local cluster 
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0 ,  ab( rating_time maturity_time ) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr'   if treasury_dummy==0,  ab( rating_time  maturity_tim cusip_6_n) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "Yes"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0,  ab( rating_time maturity_time id) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "Yes"
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0,  ab(  rating_time maturity_time issuer_time id) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "Yes"
estadd local FE4 "No"
estadd local FE5 "Yes"
esttab
esttab using  "$folder\output\return_liquidation_corp_control_zero_trading.tex", ///
 replace b(3) se(3) star(* 0.10 ** 0.05 *** 0.01  )   ///
mtitles("Return" "Return" "Return" "Return") ///
ar2  br  label nocon  nonotes  s(control FE1 FE2 FE3 FE4 FE5  N r2_a, ///
label("Control" "Rating-Time F.E."  "Maturity-Time F.E." "Issuer-Time F.E."  "Issuer F.E."  "Bond F.E."  "Observations" "Adj. R-squared") ///
 fmt(0 0  0  0 0 0 %9.0fc 3 )) width(\hsize) drop(outflow_etf zero_trading)

 set more off
eststo clear
local contr 
local y return_win 
local x   outflow outflow_etf zero_trading
local cluster 
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0 ,  ab( rating_time maturity_time ) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr'   if treasury_dummy==0,  ab( rating_time  maturity_tim cusip_6_n) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "Yes"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0,  ab( rating_time maturity_time id) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "Yes"
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0,  ab(  rating_time maturity_time issuer_time id) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
estadd local FE3 "Yes"
estadd local FE4 "No"
estadd local FE5 "Yes"
esttab
esttab using  "$folder\output\return_outflow_corp_control_zero_trading.tex", ///
 replace b(3) se(3) star(* 0.10 ** 0.05 *** 0.01  )   ///
mtitles("Return" "Return" "Return" "Return") ///
ar2  br  label nocon  nonotes  s(control FE1 FE2 FE3 FE4 FE5  N r2_a, ///
label("Control" "Rating-Time F.E."  "Maturity-Time F.E." "Issuer-Time F.E."  "Issuer F.E."  "Bond F.E."  "Observations" "Adj. R-squared") ///
 fmt(0 0  0  0 0 0 %9.0fc 3 )) width(\hsize) drop(outflow_etf zero_trading)

 
 
 set more off
eststo clear
local contr 
local y return_win 
local x   liquidation outflow_etf 
local cluster 
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0 & zero_trading_4group==1,  ab( rating_time maturity_time ) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0 & zero_trading_4group==2,  ab( rating_time maturity_time ) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0 & zero_trading_4group==3,  ab( rating_time maturity_time ) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0 & zero_trading_4group==4,  ab( rating_time maturity_time ) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
esttab
esttab using  "$folder\output\return_liquidation_corp_split_by_zero_trading.tex", ///
 replace b(3) se(3) star(* 0.10 ** 0.05 *** 0.01  )   ///
mtitles("Q1" "Q2" "Q3" "Q4") ///
ar2  br  label nocon  nonotes  s(control FE1 FE2  N r2_a, ///
label("Control" "Rating-Time F.E."  "Maturity-Time F.E."  "Observations" "Adj. R-squared") ///
 fmt(0 0  0  %9.0fc 3 )) width(\hsize) 

 
  set more off
eststo clear
local contr 
local y return_win 
local x   outflow outflow_etf 
local cluster 
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0 & zero_trading_4group==1,  ab( rating_time maturity_time ) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0 & zero_trading_4group==2,  ab( rating_time maturity_time ) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0 & zero_trading_4group==3,  ab( rating_time maturity_time ) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
eststo:reghdfe `y'  `x' `contr'  if treasury_dummy==0 & zero_trading_4group==4,  ab( rating_time maturity_time ) cluster(`cluster')
estadd local control "Yes"
estadd local FE1 "Yes"
estadd local FE2 "Yes"
esttab
esttab using  "$folder\output\return_outflow_corp_split_by_zero_trading.tex", ///
 replace b(3) se(3) star(* 0.10 ** 0.05 *** 0.01  )   ///
mtitles("Q1" "Q2" "Q3" "Q4") ///
ar2  br  label nocon  nonotes  s(control FE1 FE2  N r2_a, ///
label("Control" "Rating-Time F.E."  "Maturity-Time F.E."  "Observations" "Adj. R-squared") ///
 fmt(0 0  0 %9.0fc 3 )) width(\hsize) 

 
 
 *********************************************************************
* No fixed effects
*********************************************************************
use $\folder\data\return_security_day_level, clear

replace return_win = return_win*100 // change to basis points 
tsset id date

label var outflow "Imputed outflow"
label var outflow_etf "ETF outflow"
label var liquidation "Liquidation-adjusted outflow"
label var liquidation_etf "ETF liquidation"



encode cusip_6, g(cusip_6_n)
egen issuer_time = group(cusip_6 date)
egen maturity_at_issue_time = group(maturity_at_issue date)

keep if date>=td(01jan2020)&date<=td(31mar2020)

replace yield = yield*100
replace mf_share=mf_share*100
winsor2 yield, cut(1 99) replace
g crisis = (date>=td(1mar2020)&date<=td(10apr2020))
replace mf_share=0 if mf_share==.
replace etf_share=0 if etf_share==.
foreach var of varlist crisis  outflow liquidation {
g `var'_mf_share = `var'*mf_share
}


label var crisis_mf_share "Crisis*Mutual fund share"

 
 * Treasury (imputed outflow)
 set more off
eststo clear
local contr 
local y return_win 
local x  outflow 
local cluster date
local fe maturity_time 
eststo:reghdfe `y'  `x' `contr' if  Tbill==1,  ab(`fe' ) cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "No"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr' if on_the_run==1 & Tnote==1,  ab(`fe') cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "No"
estadd local FE3 "No"
estadd local FE4 "No"
eststo:reghdfe `y'  `x' `contr' if on_the_run==0 & Tnote==1,  ab(`fe') cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "No"
estadd local FE3 "No"
estadd local FE4 "No"
eststo:reghdfe `y'  `x' `contr' if on_the_run==1 & Tbond==1,  ab(`fe') cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "No"
estadd local FE3 "No"
estadd local FE4 "No"
eststo:reghdfe `y'  `x' `contr' if on_the_run==0 & Tbond==1,  ab(`fe') cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "No"
estadd local FE3 "No"
estadd local FE4 "No"
esttab
esttab using  "$folder\output\return_outflow_treasury_no_bond_fe_control.tex", ///
 replace b(3) se(3) star(* 0.10 ** 0.05 *** 0.01  )   ///
 mtitles("\shortstack{T-bill\\all}" "\shortstack{T-note\\on-the-run}" "\shortstack{T-note\\off-the-run}" ///
 "\shortstack{T-bond\\on-the-run}"  "\shortstack{T-bond\\off-the-run}") ///
ar2  br  label nocon  nonotes  s(control FE1 FE2  N r2_a, ///
label("Control" "Maturity-Time F.E."   "Bond F.E."  "Observations" "Adj. R-squared") ///
 fmt(0 0  0 %9.0fc 3 )) width(\hsize) 
 
 
  
 * Treasury (imputed liquidation)
set more off
eststo clear
local contr 
local y return_win 
local x  liquidation 
local cluster date
local fe maturity_time 
eststo:reghdfe `y'  `x' `contr' if  Tbill==1,  ab(`fe' ) cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "No"
estadd local FE3 "No"
estadd local FE4 "No"
estadd local FE5 "No"
eststo:reghdfe `y'  `x' `contr' if on_the_run==1 & Tnote==1,  ab(`fe') cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "No"
estadd local FE3 "No"
estadd local FE4 "No"
eststo:reghdfe `y'  `x' `contr' if on_the_run==0 & Tnote==1,  ab(`fe') cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "No"
estadd local FE3 "No"
estadd local FE4 "No"
eststo:reghdfe `y'  `x' `contr' if on_the_run==1 & Tbond==1,  ab(`fe') cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "No"
estadd local FE3 "No"
estadd local FE4 "No"
eststo:reghdfe `y'  `x' `contr' if on_the_run==0 & Tbond==1,  ab(`fe') cluster(`cluster')
estadd local control "No"
estadd local FE1 "Yes"
estadd local FE2 "No"
estadd local FE3 "No"
estadd local FE4 "No"

esttab
esttab using  "$folder\output\return_liquidation_treasury_no_bond_fe_control.tex", ///
 replace b(3) se(3) star(* 0.10 ** 0.05 *** 0.01  )   ///
 mtitles("\shortstack{T-bill\\all}"  "\shortstack{T-note\\on-the-run}" "\shortstack{T-note\\off-the-run}" ///
 "\shortstack{T-bond\\on-the-run}"  "\shortstack{T-bond\\off-the-run}") ///
ar2  br  label nocon  nonotes  s(control FE1 FE2  N r2_a, ///
label("Control" "Maturity-Time F.E."   "Bond F.E."  "Observations" "Adj. R-squared") ///
 fmt(0 0  0 %9.0fc 3 )) width(\hsize) 



*********************************************************************
* graph
*********************************************************************

/* average liquidity ranking  */
use "portno_cusip_rank_2019", clear
keep if sum_perc>=50
collapse (mean) rank [iw=weight], by(cusip)
save "cusip_rank_2019_gt50", replace

/* average liquidity ranking (index fund adjusted)  */
use "portno_cusip_rank_2019", clear
merge m:1 crsp_portno using "fund_obj"
keep if _m==3
drop _m
keep if sum_perc>=50 
drop if etf==1
sum rank if index==1
 *sum rank 
replace rank=`r(mean)' if index==1
collapse (mean) rank_index_adj=rank [iw=weight], by(cusip)
save "cusip_rank_2019_index_adj_gt50", replace



/* merge flow with return */
use  "imputed_flow", clear

merge 1:1 cusip date using "imputed_liquidation"
drop if _m==2
drop _m

merge 1:1 cusip date using "imputed_flow_etf"
drop if _m==2
drop _m

merge 1:1 cusip date using "imputed_liquidation_etf"
drop if _m==2
drop _m

merge 1:1 cusip date using "bond_ret"
g dow=dow(date)
tab dow _m 
tab dow _m  if treas==1
keep if _m==3
drop _m

merge m:1 cusip  using "cusip_rank_2019_gt50"
keep if _m==3
drop _m
merge m:1 cusip  using "cusip_rank_2019_index_adj_gt50"
keep if _m==3
drop _m

merge m:1 cusip  using "Mergent_issues_ratings"
keep if _m==3
drop _m


g maturity_year = round((maturity-td(12dec2019))/365)
*drop if maturity_year<10 & treasury_dummy==1

g outflow	= portno_flow_pct*(-100)
g outflow_etf	= portno_flow_pct_etf*(-100)

g nrank = (1-rank)
g nrank_adj = (1-rank_index_adj)

g cusip_6 = substr(cusip,1,6)

egen issuer_time_fe=group(cusip_6 date)

sum nrank, d
g nrank_dummy=(nrank>`r(p50)') 

sum nrank_adj , d
g nrank_adj_dummy=(nrank_adj>`r(p50)')

egen maturity_time = group(maturity_year date)
egen rating_time = group(rating_num date)
egen rating_maturity_time = group(rating_num maturity date)



keep if date>=td(1feb2020) & date<=td(30apr2020)

merge m:1 date maturity_year using "treasury_yield_curve"
keep if _m==3
drop _m

sum yield, d
winsor2 yield, cut(5 95) replace
replace yield=yield*100 // change to bps
replace treasury_yield=treasury_yield*100 // change to bps

g spread=yield-treasury_yield

reghdfe yield , ab( FE2=rating_time)
predict u, res
reghdfe spread if treasury_dummy==0, ab( FE=rating_time)
predict u2, res

sum rank_index_adj, d

g rank_category = 4 if rank_index_adj<`r(p25)'
replace rank_category = 3 if rank_index_adj>=`r(p25)' & rank_index_adj<`r(p50)'
replace rank_category = 2 if rank_index_adj>=`r(p50)' & rank_index_adj<`r(p75)'
replace rank_category = 1 if rank_index_adj>=`r(p75)'& rank_index_adj<1

collapse (median) yld_pt=u spread=u2 , by(rank_category date)
tsset rank_category date


bysort rank_category: g yld_pt_norm = yld_pt-yld_pt[1]
bysort rank_category: g spread_norm = spread-spread[1]


twoway (tsline yld_pt_norm if rank_category==4) (tsline yld_pt_norm if rank_category==3, lpattern("--")) (tsline yld_pt_norm if rank_category==2) (tsline yld_pt_norm if rank_category==1, lpattern("--")), ///
legend(order(1 2 3 4) label(1 "Q1") label(2 "Q2") label(3 "Q3") label(4 "Q4") )  ylabel(, nogrid)  title("")  ///
graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
xtitle("") ytitle("Yield (bps)")  xlabel(,format(%tdm)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\bond_yield_by_rank_4group.pdf", replace

twoway (tsline spread_norm if rank_category==4) (tsline spread_norm if rank_category==3, lpattern("--")) (tsline spread_norm if rank_category==2) (tsline spread_norm if rank_category==1, lpattern("--")), ///
legend(order(1 2 3 4) label(1 "Q1") label(2 "Q2") label(3 "Q3") label(4 "Q4") )  ylabel(, nogrid)  title("")  ///
graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
xtitle("") ytitle("Yield (bps)")  xlabel(,format(%tdm)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\cbond_spread_by_rank_4group.pdf", replace



* aggregate holding states
*use "$folder/data\security_level_analysis/holdings_2019Q4_2020Q2.dta" if report_dt==td(31dec2019), clear
use "$folder/data\security_level_analysis/holdings_2019Q4_2020Q2.dta" , clear
merge m:1 cusip  using "Mergent_issues_ratings"
keep if _m==3
drop _m

merge m:1 crsp_portno using "fund_obj"
keep if _m==3
drop _m
drop if etf_dummy==1
drop if substr(crsp_obj_cd,1,2)=="IG"

g bond_category="Treasury" if substr(bond_type,1,1)=="U"|substr(bond_type,1,1)=="O"
replace bond_category="Agency" if substr(bond_type,1,2)=="AD"|substr(bond_type,1,2)=="AM"| ///
substr(bond_type,1,2)=="AR"|substr(bond_type,1,2)=="AS"
replace bond_category="Corp" if substr(bond_type,1,1)=="C"

g maturity_year = round((maturity - td(31dec2019))/365)

g bond_category2=bond_category
replace bond_category2="Tbill" if bond_category=="Treasury" & maturity_year<1 
replace bond_category2="Tnote" if bond_category=="Treasury" & maturity_year<10&maturity_year>=1 
replace bond_category2="Tbond" if bond_category=="Treasury" & maturity_year>=10 
replace bond_category2="Others" if bond_category2==""

collapse (sum) market_val, by(bond_category2 report_dt)
bysort report_dt: egen total_market_val=sum(market_val)

g share = market_val/total_market_val

tab bond, sum(share)
 
save fund_position, replace







* treasury yield by mutual fund holding
use $\folder\data\return_security_day_level, clear

keep if date>=td(1feb2020) & date<=td(30apr2020)
keep if treasury==1
sum yield, d
winsor2 yield, cut(5 95) replace
reghdfe yield , ab( FE2=date FE1=id)
predict u, res

sum mf_share , d

g rank_category = 1 if mf_share<`r(p25)'
replace rank_category = 2 if mf_share>=`r(p25)' & mf_share<`r(p50)'
replace rank_category = 3 if mf_share>=`r(p50)' & mf_share<`r(p75)'
replace rank_category = 4 if mf_share>=`r(p75)'& mf_share<=1

collapse (median) yld_pt=u , by(rank_category date)
tsset rank_category date

drop if missing(yld_pt)
replace yld_pt=yld_pt*100 // change to bps


bysort rank_category: g yld_pt_norm = yld_pt-yld_pt[1]

sum yld_pt_norm if rank_category==4 & date==td(19march2020) // for paper discussion
sum yld_pt_norm if rank_category==1 & date==td(19march2020) // for paper discussion

sum yld_pt_norm if rank_category==4 & date==td(20march2020) // for paper discussion
sum yld_pt_norm if rank_category==1 & date==td(20march2020) // for paper discussion

twoway (tsline yld_pt_norm if rank_category==4) (tsline yld_pt_norm if rank_category==3, lpattern("--")) (tsline yld_pt_norm if rank_category==2) (tsline yld_pt_norm if rank_category==1, lpattern("--")), ///
legend(order(1 2 3 4) label(1 "Q1") label(2 "Q2") label(3 "Q3") label(4 "Q4") )  ylabel(, nogrid)  title("")  ///
graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
xtitle("") ytitle("Yield (bps)")  xlabel(,format(%tdm)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$dropbox\draft\figures\treasury_yield_by_mfshare_4group.pdf", replace




* corporate bond yield by mutual fund holding
use $\folder\data\return_security_day_level, clear

keep if date>=td(1feb2020) & date<=td(30apr2020)
keep if treasury==0

merge m:1 date maturity_year using "treasury_yield_curve"
keep if _m==3
drop _m

replace yield=yield*100 // change to bps
replace treasury_yield=treasury_yield*100 // change to bps

sum yield, d
winsor2 yield, cut(5 95) replace

g spread = yield - treasury_yield

reghdfe yield , ab( FE2=rating_time )
predict u, res

reghdfe spread , ab( FE=rating_time )
predict u2, res

sum mf_share , d


g rank_category = 1 if mf_share<`r(p25)'
replace rank_category = 2 if mf_share>=`r(p25)' & mf_share<`r(p50)'
replace rank_category = 3 if mf_share>=`r(p50)' & mf_share<`r(p75)'
replace rank_category = 4 if mf_share>=`r(p75)'& mf_share<1

collapse (median) yld_pt=u spread=u2 , by(rank_category date)
tsset rank_category date

drop if missing(yld_pt)


bysort rank_category: g yld_pt_norm = yld_pt-yld_pt[1]
bysort rank_category: g spread_norm = spread-spread[1]


twoway (tsline yld_pt_norm if rank_category==4) (tsline yld_pt_norm if rank_category==3, lpattern("--")) (tsline yld_pt_norm if rank_category==2) (tsline yld_pt_norm if rank_category==1, lpattern("--")), ///
legend(order(1 2 3 4) label(1 "Q1") label(2 "Q2") label(3 "Q3") label(4 "Q4") )  ylabel(, nogrid)  title("")  ///
graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
xtitle("") ytitle("Yield (bps)")  xlabel(,format(%tdm)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\cbond_yield_by_mfshare_4group.pdf", replace

twoway (tsline spread_norm if rank_category==4) (tsline spread_norm if rank_category==3, lpattern("--")) (tsline spread_norm if rank_category==2) (tsline spread_norm if rank_category==1, lpattern("--")), ///
legend(order(1 2 3 4) label(1 "Q1") label(2 "Q2") label(3 "Q3") label(4 "Q4") )  ylabel(, nogrid)  title("")  ///
graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
xtitle("") ytitle("Yield (bps)")  xlabel(,format(%tdm)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\cbond_spread_by_mfshare_4group.pdf", replace


/*


freduse DGS1MO DGS1 DGS2 DGS3 DGS5 DGS7 DGS10 DGS20 DGS30, clear 
drop date
rename daten date
keep if year(date)==2020

reshape long DGS, i(date) j(maturity) string
g maturity_year = .

replace maturity_year = 0 if maturity=="1MO"

foreach num of numlist 1 2 3 5 7 10 20 30 {
replace maturity_year = `num' if maturity=="`num'"
}

tsset date maturity_year
tsfill

by date: ipolate DGS maturity_year, generate(treasury_yield) epolate
keep date maturity_year treasury_yield
save treasury_yield_curve, replace
*/
*************************************************
* corporate bond yield by mutual fund holding (control for trading frequency)
*************************************************
use "liquidity_bond_tm", clear
keep if date>=tm(2020m1) 
collapse (mean) turnover-size_neg num_transaction, by(cusip_id tm)
g cusip_8 = substr(cusip, 1, 8)
drop cusip_id
rename cusip_8 cusip
save "liquidity_2020", replace


use $\folder\data\return_security_day_level, clear
drop tm
g tm = mofd(date)
format tm %tm
merge m:1 cusip tm using "liquidity_2020"
drop if _m==2
drop _m
keep if date>=td(1feb2020) & date<=td(30apr2020)
keep if treasury==0

label var zero_trading "Zero-trading days"
label var num_transaction "Number of transactions"
label var Amihud "Amihud"
label var Roll "Roll"
label var IRC "IRC"

estpost summarize zero_trading num_transaction Amihud  Roll IRC  ///
 , d
est store sum_stat
esttab sum_stat using "$folder\output\summary_stats_bond_liquidity.tex", replace ///
cells("mean(fmt(%9.3f)) sd(fmt(%9.3f))  p10(fmt(%9.3f)) p25(fmt(%9.3f)) p50(fmt(%9.3f)) p75(fmt(%9.3f)) p90(fmt(%9.3f)) ") ///
label booktabs nonum gaps noobs ///
collabels( "mean" "sd" "p10" "p25" "p50" "p75" "p90") width(\hsize) 


use $\folder\data\return_security_day_level, clear
drop tm
g tm = mofd(date)
format tm %tm
merge m:1 cusip tm using "liquidity_2020"
drop if _m==2
drop _m

keep if date>=td(1feb2020) & date<=td(30apr2020)
keep if treasury==0

sum 
sum yield, d
winsor2 yield, cut(5 95) replace
reghdfe yield zero_trading, ab( FE2=rating_time )
predict u, res

sum mf_share , d
sum zero_trading if rating_num>=1 & rating_num!=.
sum zero_trading if rating_num<1 & rating_num!=.

g rank_category = 1 if mf_share<`r(p25)'
replace rank_category = 2 if mf_share>=`r(p25)' & mf_share<`r(p50)'
replace rank_category = 3 if mf_share>=`r(p50)' & mf_share<`r(p75)'
replace rank_category = 4 if mf_share>=`r(p75)'& mf_share<1

collapse (median) yld_pt=u , by(rank_category date)
tsset rank_category date

drop if missing(yld_pt)
replace yld_pt=yld_pt*100 // change to bps


bysort rank_category: g yld_pt_norm = yld_pt-yld_pt[1]

sum yld_pt_norm if rank_category==4 & date==td(19march2020) // for paper discussion
sum yld_pt_norm if rank_category==1 & date==td(19march2020) // for paper discussion

sum yld_pt_norm if rank_category==4 & date==td(20march2020) // for paper discussion
sum yld_pt_norm if rank_category==1 & date==td(20march2020) // for paper discussion


twoway (tsline yld_pt_norm if rank_category==4) (tsline yld_pt_norm if rank_category==3, lpattern("--")) (tsline yld_pt_norm if rank_category==2) (tsline yld_pt_norm if rank_category==1, lpattern("--")), ///
legend(order(1 2 3 4) label(1 "Q1") label(2 "Q2") label(3 "Q3") label(4 "Q4") )  ylabel(, nogrid)  title("")  ///
graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
xtitle("") ytitle("Yield (bps)")  xlabel(,format(%tdm)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\cbond_yield_by_mfshare_4group_zero_trading.pdf", replace


*************************************************
*  yield by mutual fund holding without extracing fixed effects
*************************************************
* treasury yield by mutual fund holding
use $\folder\data\return_security_day_level, clear

keep if date>=td(1feb2020) & date<=td(30apr2020)
keep if treasury==1
sum yield, d
winsor2 yield, cut(5 95) replace
reghdfe yield , ab( FE2=date FE1=id)
predict u, res

sum mf_share , d

g rank_category = 1 if mf_share<`r(p25)'
replace rank_category = 2 if mf_share>=`r(p25)' & mf_share<`r(p50)'
replace rank_category = 3 if mf_share>=`r(p50)' & mf_share<`r(p75)'
replace rank_category = 4 if mf_share>=`r(p75)'& mf_share<=1

collapse (median) yld_pt=yield , by(rank_category date)
tsset rank_category date

drop if missing(yld_pt)
replace yld_pt=yld_pt*100 // change to bps


bysort rank_category: g yld_pt_norm = yld_pt-yld_pt[1]

sum yld_pt_norm if rank_category==4 & date==td(19march2020) // for paper discussion
sum yld_pt_norm if rank_category==1 & date==td(19march2020) // for paper discussion

sum yld_pt_norm if rank_category==4 & date==td(20march2020) // for paper discussion
sum yld_pt_norm if rank_category==1 & date==td(20march2020) // for paper discussion

twoway (tsline yld_pt_norm if rank_category==4) (tsline yld_pt_norm if rank_category==3, lpattern("--")) (tsline yld_pt_norm if rank_category==2) (tsline yld_pt_norm if rank_category==1, lpattern("--")), ///
legend(order(1 2 3 4) label(1 "Q1") label(2 "Q2") label(3 "Q3") label(4 "Q4") )  ylabel(, nogrid)  title("")  ///
graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
xtitle("") ytitle("Yield (bps)")  xlabel(,format(%tdm)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\treasury_yield_by_mfshare_4group_no_fe.pdf", replace




* corporate bond yield by mutual fund holding
use $\folder\data\return_security_day_level, clear

keep if date>=td(1feb2020) & date<=td(30apr2020)
keep if treasury==0
sum yield, d
winsor2 yield, cut(5 95) replace
reghdfe yield , ab( FE2=rating_time )
predict u, res

sum mf_share , d


g rank_category = 1 if mf_share<`r(p25)'
replace rank_category = 2 if mf_share>=`r(p25)' & mf_share<`r(p50)'
replace rank_category = 3 if mf_share>=`r(p50)' & mf_share<`r(p75)'
replace rank_category = 4 if mf_share>=`r(p75)'& mf_share<1

collapse (median) yld_pt=yield , by(rank_category date)
tsset rank_category date

drop if missing(yld_pt)
replace yld_pt=yld_pt*100 // change to bps


bysort rank_category: g yld_pt_norm = yld_pt-yld_pt[1]

sum yld_pt_norm if rank_category==4 & date==td(19march2020) // for paper discussion
sum yld_pt_norm if rank_category==1 & date==td(19march2020) // for paper discussion

sum yld_pt_norm if rank_category==4 & date==td(20march2020) // for paper discussion
sum yld_pt_norm if rank_category==1 & date==td(20march2020) // for paper discussion


twoway (tsline yld_pt_norm if rank_category==4) (tsline yld_pt_norm if rank_category==3, lpattern("--")) (tsline yld_pt_norm if rank_category==2) (tsline yld_pt_norm if rank_category==1, lpattern("--")), ///
legend(order(1 2 3 4) label(1 "Q1") label(2 "Q2") label(3 "Q3") label(4 "Q4") )  ylabel(, nogrid)  title("")  ///
graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
xtitle("") ytitle("Yield (bps)")  xlabel(,format(%tdm)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\cbond_yield_by_mfshare_4group_no_fe.pdf", replace

***************************************
* time-varying loading on mutual fund ownership (wo controls)
***************************************
* Treasury yield by mutual fund holding 
use $\folder\data\return_security_day_level, clear

keep if date>=td(1feb2020) & date<=td(30apr2020)
sum maturity_year if treasury==1, d
sum maturity_year if treasury==0, d

keep if treasury==1
sum yield, d
replace yield=yield*100 // change to bps
replace mf_share=mf_share*100 // change to percent of outstanding
sum mf_share, d

sum maturity_year, d
g ST = maturity_year<5

g b_mf_share=.
g yield_counter = .
g share_r2 = .
levelsof date, local(levels) 
 foreach l of local levels {
  winsor2 yield if date==`l', cut(5 95) replace
 reg yield mf_share     if date == `l'
 replace b_mf_share=_b[mf_share] if date == `l' 
 replace yield_counter=yield-mf_share*_b[mf_share] if date == `l' 

 }


sum yield yield_counter maturity_year if mofd(date)==tm(2020m3)
 g diff = mf_share*b_mf_share
 
collapse (median) yield_counter yield b_mf_share (median) diff_50=diff ///
(p25) diff_25=diff (p75) diff_75=diff (p10) diff_10=diff (p90) diff_90=diff, by( date)
tsset  date

sum yield yield_counter if mofd(date)==tm(2020m3)

foreach var of varlist diff_90 diff_75 diff_50 diff_25 diff_10 {
g `var'_norm=`var'-`var'[1]
}

twoway (tsline diff_90 diff_75 diff_50 diff_25 diff_10, lpatter(solid "-" "--" "." "." ))   , ///
legend(order(1 2 3 4 5) label(1 "p90") label(2 "p75") label(3 "p50") label(4 "p25")  label(5 "p10"))  ylabel(, nogrid)  title("")  ///
graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
xtitle("") ytitle("Effects of mutual fund ownership on yields (bps)")  xlabel(,format(%tdm)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\counterfactual_treasury_no_control_win_95.pdf", replace


* corporate bond yield by mutual fund holding (IG)
use $\folder\data\return_security_day_level, clear
keep if date>=td(1feb2020) & date<=td(30apr2020)
keep if treasury==0
keep if rating_num>=1 & rating_num!=.
sum yield, d
*winsor2 yield, cut(5 95) replace
replace yield=yield*100 // change to bps
replace mf_share=mf_share*100 // change to percent of outstanding
sum mf_share, d

foreach var of varlist rating_num   {
tabulate `var', generate(dummy_`var')
}
g ST = maturity_year<5

g b_mf_share=.
g yield_counter = .
levelsof date, local(levels) 
 foreach l of local levels {
  winsor2 yield if date==`l', cut(5 95) replace
 reg yield mf_share  if date == `l'
 replace b_mf_share=_b[mf_share] if date == `l' 
 replace yield_counter=yield-mf_share*_b[mf_share] if date == `l' 
 }

 g diff = mf_share*b_mf_share
 
collapse (median) yield_counter yield b_mf_share (median) diff_50=diff ///
(p25) diff_25=diff (p75) diff_75=diff (p10) diff_10=diff (p90) diff_90=diff, by( date)
tsset  date

sum yield yield_counter if mofd(date)==tm(2020m3)

foreach var of varlist diff_90 diff_75 diff_50 diff_25 diff_10 {
g `var'_norm=`var'-`var'[1]
}

twoway (tsline diff_90 diff_75 diff_50 diff_25 diff_10, lpatter(solid "-" "--" "." "." ))   , ///
legend(order(1 2 3 4 5) label(1 "p90") label(2 "p75") label(3 "p50") label(4 "p25")  label(5 "p10"))  ylabel(, nogrid)  title("")  ///
graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
xtitle("") ytitle("Effects of mutual fund ownership on yields (bps)")  xlabel(,format(%tdm)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\counterfactual_IG_no_control_win_95.pdf", replace



* corporate bond yield by mutual fund holding (HY)
use $\folder\data\return_security_day_level, clear
keep if date>=td(1feb2020) & date<=td(30apr2020)
keep if treasury==0
keep if rating_num<=0 & rating_num!=.
drop if rating_num<-10
sum yield, d
*winsor2 yield, cut(5 95) replace
replace yield=yield*100 // change to bps
replace mf_share=mf_share*100 // change to percent of outstanding
sum mf_share, d
sum maturity_year, d
foreach var of varlist rating_num   {
tabulate `var', generate(dummy_`var')
}

g ST = maturity_year<5

g b_mf_share=.
g yield_counter = .
levelsof date, local(levels) 
 foreach l of local levels {
 winsor2 yield if date==`l', cut(5 95) replace
 reg yield mf_share   if date == `l'
 replace b_mf_share=_b[mf_share] if date == `l' 
 replace yield_counter=yield-mf_share*_b[mf_share] if date == `l' 
 }

 g diff = mf_share*b_mf_share
 
collapse (median) yield_counter yield b_mf_share (median) diff_50=diff ///
(p25) diff_25=diff (p75) diff_75=diff (p10) diff_10=diff (p90) diff_90=diff, by( date)
tsset  date

sum yield yield_counter if mofd(date)==tm(2020m3)

foreach var of varlist diff_90 diff_75 diff_50 diff_25 diff_10 {
g `var'_norm=`var'-`var'[1]
}

twoway (tsline diff_90 diff_75 diff_50 diff_25 diff_10, lpatter(solid "-" "--" "." "." ))   , ///
legend(order(1 2 3 4 5)  label(1 "p90")  label(2 "p75") label(3 "p50") label(4 "p25") label(5 "p10"))  ylabel(, nogrid)  title("")  ///
graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
xtitle("") ytitle("Effects of mutual fund ownership on yields (bps)")  xlabel(,format(%tdm)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\counterfactual_HY_no_control_win_95.pdf", replace




***************************************
* time-varying loading on mutual fund ownership (with controls)
***************************************
* Treasury yield by mutual fund holding 
use $\folder\data\return_security_day_level, clear

keep if date>=td(1feb2020) & date<=td(30apr2020)
sum maturity_year if treasury==1, d
sum maturity_year if treasury==0, d

keep if treasury==1
sum yield, d
replace yield=yield*100 // change to bps
replace mf_share=mf_share*100 // change to percent of outstanding
sum mf_share, d

sum maturity_year, d
g ST = maturity_year<5

g b_mf_share=.
g yield_counter = .
g share_r2 = .
levelsof date, local(levels) 
 foreach l of local levels {
  winsor2 yield if date==`l', cut(5 95) replace
 reg yield mf_share ST    if date == `l'
 replace b_mf_share=_b[mf_share] if date == `l' 
 replace yield_counter=yield-mf_share*_b[mf_share] if date == `l' 

 }


sum yield yield_counter maturity_year if mofd(date)==tm(2020m3)
 g diff = mf_share*b_mf_share
 
collapse (median) yield_counter yield b_mf_share (median) diff_50=diff ///
(p25) diff_25=diff (p75) diff_75=diff (p10) diff_10=diff (p90) diff_90=diff, by( date)
tsset  date

sum yield yield_counter if mofd(date)==tm(2020m3)

foreach var of varlist diff_90 diff_75 diff_50 diff_25 diff_10 {
g `var'_norm=`var'-`var'[1]
}

twoway (tsline diff_90 diff_75 diff_50 diff_25 diff_10, lpatter(solid "-" "--" "." "." ))   , ///
legend(order(1 2 3 4 5) label(1 "p90") label(2 "p75") label(3 "p50") label(4 "p25")  label(5 "p10"))  ylabel(, nogrid)  title("")  ///
graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
xtitle("") ytitle("Effects of mutual fund ownership on yields (bps)")  xlabel(,format(%tdm)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\counterfactual_treasury_wt_control_win_95.pdf", replace


* corporate bond yield by mutual fund holding (IG)
use $\folder\data\return_security_day_level, clear
keep if date>=td(1feb2020) & date<=td(30apr2020)
keep if treasury==0
keep if rating_num>=1 & rating_num!=.
sum yield, d
*winsor2 yield, cut(5 95) replace
replace yield=yield*100 // change to bps
replace mf_share=mf_share*100 // change to percent of outstanding
sum mf_share, d

foreach var of varlist rating_num   {
tabulate `var', generate(dummy_`var')
}
g ST = maturity_year<5

g b_mf_share=.
g yield_counter = .
levelsof date, local(levels) 
 foreach l of local levels {
  winsor2 yield if date==`l', cut(5 95) replace
 reg yield mf_share ST if date == `l'
 replace b_mf_share=_b[mf_share] if date == `l' 
 replace yield_counter=yield-mf_share*_b[mf_share] if date == `l' 
 }

 g diff = mf_share*b_mf_share
 
collapse (median) yield_counter yield b_mf_share (median) diff_50=diff ///
(p25) diff_25=diff (p75) diff_75=diff (p10) diff_10=diff (p90) diff_90=diff, by( date)
tsset  date

sum yield yield_counter if mofd(date)==tm(2020m3)

foreach var of varlist diff_90 diff_75 diff_50 diff_25 diff_10 {
g `var'_norm=`var'-`var'[1]
}

twoway (tsline diff_90 diff_75 diff_50 diff_25 diff_10, lpatter(solid "-" "--" "." "." ))   , ///
legend(order(1 2 3 4 5) label(1 "p90") label(2 "p75") label(3 "p50") label(4 "p25")  label(5 "p10"))  ylabel(, nogrid)  title("")  ///
graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
xtitle("") ytitle("Effects of mutual fund ownership on yields (bps)")  xlabel(,format(%tdm)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\counterfactual_IG_wt_control_win_95.pdf", replace



* corporate bond yield by mutual fund holding (HY)
use $\folder\data\return_security_day_level, clear
keep if date>=td(1feb2020) & date<=td(30apr2020)
keep if treasury==0
keep if rating_num<=0 & rating_num!=.
drop if rating_num<-10
sum yield, d
*winsor2 yield, cut(5 95) replace
replace yield=yield*100 // change to bps
replace mf_share=mf_share*100 // change to percent of outstanding
sum mf_share, d
sum maturity_year, d
foreach var of varlist rating_num   {
tabulate `var', generate(dummy_`var')
}

g ST = maturity_year<5

g b_mf_share=.
g yield_counter = .
levelsof date, local(levels) 
 foreach l of local levels {
 winsor2 yield if date==`l', cut(5 95) replace
 reg yield mf_share  ST if date == `l'
 replace b_mf_share=_b[mf_share] if date == `l' 
 replace yield_counter=yield-mf_share*_b[mf_share] if date == `l' 
 }

 g diff = mf_share*b_mf_share
 
collapse (median) yield_counter yield b_mf_share (median) diff_50=diff ///
(p25) diff_25=diff (p75) diff_75=diff (p10) diff_10=diff (p90) diff_90=diff, by( date)
tsset  date

sum yield yield_counter if mofd(date)==tm(2020m3)

foreach var of varlist diff_90 diff_75 diff_50 diff_25 diff_10 {
g `var'_norm=`var'-`var'[1]
}

twoway (tsline diff_90 diff_75 diff_50 diff_25 diff_10, lpatter(solid "-" "--" "." "." ))   , ///
legend(order(1 2 3 4 5)  label(1 "p90")  label(2 "p75") label(3 "p50") label(4 "p25") label(5 "p10"))  ylabel(, nogrid)  title("")  ///
graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
xtitle("") ytitle("Effects of mutual fund ownership on yields (bps)")  xlabel(,format(%tdm)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\counterfactual_HY_wt_control_win_95.pdf", replace







********************************************
* corporate bond yield by mutual fund holding (IG vs HY)
********************************************
* IG <5year
use $\folder\data\return_security_day_level, clear

keep if date>=td(1feb2020) & date<=td(30apr2020)
keep if treasury==0
keep if rating_num>=1 & rating_num!=. // IG bonds
keep if maturity_year<5

sum yield, d
winsor2 yield, cut(5 95) replace
reghdfe yield , ab( FE2=rating_time )
predict u, res

sum mf_share , d


g rank_category = 1 if mf_share<`r(p25)'
replace rank_category = 2 if mf_share>=`r(p25)' & mf_share<`r(p50)'
replace rank_category = 3 if mf_share>=`r(p50)' & mf_share<`r(p75)'
replace rank_category = 4 if mf_share>=`r(p75)'& mf_share<1

collapse (median) yld_pt=u , by(rank_category date)
tsset rank_category date

drop if missing(yld_pt)
replace yld_pt=yld_pt*100 // change to bps


bysort rank_category: g yld_pt_norm = yld_pt-yld_pt[1]

sum yld_pt_norm if rank_category==4 & date==td(19march2020) // for paper discussion
sum yld_pt_norm if rank_category==1 & date==td(19march2020) // for paper discussion

sum yld_pt_norm if rank_category==4 & date==td(20march2020) // for paper discussion
sum yld_pt_norm if rank_category==1 & date==td(20march2020) // for paper discussion


twoway (tsline yld_pt_norm if rank_category==4) (tsline yld_pt_norm if rank_category==3, lpattern("--")) (tsline yld_pt_norm if rank_category==2) (tsline yld_pt_norm if rank_category==1, lpattern("--")), ///
legend(order(1 2 3 4) label(1 "Q1") label(2 "Q2") label(3 "Q3") label(4 "Q4") )  ylabel(, nogrid)  title("")  ///
graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
xtitle("") ytitle("Yield (bps)")  xlabel(,format(%tdm)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\cbond_yield_by_mfshare_4group_IG_below_5.pdf", replace



* IG >=5year
use $\folder\data\return_security_day_level, clear

keep if date>=td(1feb2020) & date<=td(30apr2020)
keep if treasury==0
keep if rating_num>=1 & rating_num!=. // IG bonds
keep if maturity_year>=5

sum yield, d
winsor2 yield, cut(5 95) replace
reghdfe yield , ab( FE2=rating_time )
predict u, res

sum mf_share , d


g rank_category = 1 if mf_share<`r(p25)'
replace rank_category = 2 if mf_share>=`r(p25)' & mf_share<`r(p50)'
replace rank_category = 3 if mf_share>=`r(p50)' & mf_share<`r(p75)'
replace rank_category = 4 if mf_share>=`r(p75)'& mf_share<1

collapse (median) yld_pt=u , by(rank_category date)
tsset rank_category date

drop if missing(yld_pt)
replace yld_pt=yld_pt*100 // change to bps


bysort rank_category: g yld_pt_norm = yld_pt-yld_pt[1]

sum yld_pt_norm if rank_category==4 & date==td(19march2020) // for paper discussion
sum yld_pt_norm if rank_category==1 & date==td(19march2020) // for paper discussion

sum yld_pt_norm if rank_category==4 & date==td(20march2020) // for paper discussion
sum yld_pt_norm if rank_category==1 & date==td(20march2020) // for paper discussion


twoway (tsline yld_pt_norm if rank_category==4) (tsline yld_pt_norm if rank_category==3, lpattern("--")) (tsline yld_pt_norm if rank_category==2) (tsline yld_pt_norm if rank_category==1, lpattern("--")), ///
legend(order(1 2 3 4) label(1 "Q1") label(2 "Q2") label(3 "Q3") label(4 "Q4") )  ylabel(, nogrid)  title("")  ///
graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
xtitle("") ytitle("Yield (bps)")  xlabel(,format(%tdm)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\cbond_yield_by_mfshare_4group_IG_above_5.pdf", replace



* HY <5year
use $\folder\data\return_security_day_level, clear

keep if date>=td(1feb2020) & date<=td(30apr2020)
keep if treasury==0
keep if rating_num<1 & rating_num!=. // HY bonds
drop if rating_num<=-10
keep if maturity_year<5
 
drop if mf_share ==.
sum yield, d
winsor2 yield, cut(5 95) replace
reghdfe yield , ab( FE2=rating_time )
predict u, res

sum mf_share , d

/*
g rank_category = 1 
*/

g rank_category = 1 if mf_share<`r(p25)'
replace rank_category = 2 if mf_share>=`r(p25)' & mf_share<`r(p50)'
replace rank_category = 3 if mf_share>=`r(p50)' & mf_share<`r(p75)'
replace rank_category = 4 if mf_share>=`r(p75)'& mf_share<1

collapse (median) yld_pt=u , by(rank_category date)
tsset rank_category date

drop if missing(yld_pt)
replace yld_pt=yld_pt*100 // change to bps


bysort rank_category: g yld_pt_norm = yld_pt-yld_pt[1]

sum yld_pt_norm if rank_category==4 & date==td(19march2020) // for paper discussion
sum yld_pt_norm if rank_category==1 & date==td(19march2020) // for paper discussion

sum yld_pt_norm if rank_category==4 & date==td(20march2020) // for paper discussion
sum yld_pt_norm if rank_category==1 & date==td(20march2020) // for paper discussion


twoway (tsline yld_pt_norm if rank_category==4) (tsline yld_pt_norm if rank_category==3, lpattern("--")) (tsline yld_pt_norm if rank_category==2) (tsline yld_pt_norm if rank_category==1, lpattern("--")), ///
legend(order(1 2 3 4) label(1 "Q1") label(2 "Q2") label(3 "Q3") label(4 "Q4") )  ylabel(, nogrid)  title("")  ///
graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
xtitle("") ytitle("Yield (bps)")  xlabel(,format(%tdm)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\cbond_yield_by_mfshare_HY_below_5.pdf", replace



* HY >=5year
use $\folder\data\return_security_day_level, clear

keep if date>=td(1feb2020) & date<=td(30apr2020)
keep if treasury==0
keep if rating_num>=1 & rating_num!=. // IG bonds
keep if maturity_year>=5

sum yield, d
winsor2 yield, cut(5 95) replace
reghdfe yield , ab( FE2=rating_time )
predict u, res

sum mf_share , d


g rank_category = 1 if mf_share<`r(p25)'
replace rank_category = 2 if mf_share>=`r(p25)' & mf_share<`r(p50)'
replace rank_category = 3 if mf_share>=`r(p50)' & mf_share<`r(p75)'
replace rank_category = 4 if mf_share>=`r(p75)'& mf_share<1

collapse (median) yld_pt=u , by(rank_category date)
tsset rank_category date

drop if missing(yld_pt)
replace yld_pt=yld_pt*100 // change to bps


bysort rank_category: g yld_pt_norm = yld_pt-yld_pt[1]

sum yld_pt_norm if rank_category==4 & date==td(19march2020) // for paper discussion
sum yld_pt_norm if rank_category==1 & date==td(19march2020) // for paper discussion

sum yld_pt_norm if rank_category==4 & date==td(20march2020) // for paper discussion
sum yld_pt_norm if rank_category==1 & date==td(20march2020) // for paper discussion


twoway (tsline yld_pt_norm if rank_category==4) (tsline yld_pt_norm if rank_category==3, lpattern("--")) (tsline yld_pt_norm if rank_category==2) (tsline yld_pt_norm if rank_category==1, lpattern("--")), ///
legend(order(1 2 3 4) label(1 "Q1") label(2 "Q2") label(3 "Q3") label(4 "Q4") )  ylabel(, nogrid)  title("")  ///
graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
xtitle("") ytitle("Yield (bps)")  xlabel(,format(%tdm)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\cbond_yield_by_mfshare_4group_HY_above_5.pdf", replace




*********************************************************
* yield by ownership quartile (IG vs. HY, ST vs. LT)
use $\folder\data\return_security_day_level, clear

keep if date>=td(1feb2020) & date<=td(30apr2020)
keep if treasury==0
keep if rating_num>=-10
g IG = (rating_num>=1 & rating_num!=.)
g ST = (maturity_year<5)
egen group = group(IG ST)
egen group_time = group(group date)

drop if mf_share ==.
drop if maturity_year <0 | maturity_year==.

replace yield=yield*100 // change to bps
sum yield, d
winsor2 yield, cut(5 95) replace
*reghdfe yield , ab( FE2=rating_time )
reghdfe yield , ab( FE2=group_time )
predict u, res

g rank_category=.

foreach i of numlist 0/1 {
foreach j of numlist 0/1 {
sum mf_share if IG==`i' & ST==`j', d
replace rank_category = 1 if mf_share<`r(p25)'& IG==`i' & ST==`j'
replace rank_category = 2 if mf_share>=`r(p25)' & mf_share<`r(p50)'& IG==`i' & ST==`j'
replace rank_category = 3 if mf_share>=`r(p50)' & mf_share<`r(p75)'& IG==`i' & ST==`j'
replace rank_category = 4 if mf_share>=`r(p75)'& mf_share<1& IG==`i' & ST==`j'
}
}
drop if missing(rank_category)
collapse (median)  yield yld_pt=u , by(IG ST group rank_category date)
reshape wide yield yld_pt,i(IG ST group date) j(rank)

tsset group date

bysort group: g diff = (yld_pt4 - yld_pt1) - (yld_pt4[1] - yld_pt1[1])

twoway (tsline diff if IG==1 & ST==1) ///
(tsline diff if IG==1 & ST==0, lpattern("--")) ///
(tsline diff if IG==0 & ST==1, lpattern(".") ) ///
(tsline diff if IG==0 & ST==0, lpattern("-.")), ///
legend(order(1 2 3 4) label(1 "IG-ST") label(2 "IG-LT") label(3 "HY-ST") label(4 "HY-LT") )  ylabel(, nogrid)  title("")  ///
graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
xtitle("") ytitle("Yield (bps)")  xlabel(,format(%tdm)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\cbond_yield_by_mfshare_rating_maturity.pdf", replace


bysort group: g march23 = diff-diff[_n-1] if date==td(23mar2020)
bysort group: g april9 = diff-diff[_n-1] if date==td(09apr2020)
sum march23 april9 if IG==1 & ST==1
sum march23 april9 if IG==1 & ST==0
sum march23 april9 if IG==0 & ST==1
sum march23 april9 if IG==0 & ST==0

twoway (tsline diff if IG==1 & ST==1) ///
(tsline diff if IG==1 & ST==0, lpattern("--")) ///
,legend(order(1 2 ) label(1 "Maturity<5") label(2 "Maturity>5")  )  ylabel(, nogrid)  title("")  ///
graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
xtitle("") ytitle("Yield (bps)")  xlabel(,format(%tdm)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\cbond_yield_by_mfshare_rating_maturity_IG.pdf", replace

twoway (tsline diff if IG==0 & ST==1) ///
(tsline diff if IG==0 & ST==0, lpattern("--")) ///
,legend(order(1 2 ) label(1 "Maturity<5") label(2 "Maturity>5")  )  ylabel(, nogrid)  title("")  ///
graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
xtitle("") ytitle("Yield (bps)")  xlabel(,format(%tdm)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\cbond_yield_by_mfshare_rating_maturity_HY.pdf", replace

save Figure_cbond_yield_by_mfshare_rating_maturity_raw_data, replace


*********************************************************
* yield by rank quartile (IG vs. HY, ST vs. LT)
use $\folder\data\return_security_day_level, clear

keep if date>=td(1feb2020) & date<=td(30apr2020)
keep if treasury==0
keep if rating_num>=-10
g IG = (rating_num>=1 & rating_num!=.)
g ST = (maturity_year<=5)
 egen group = group(IG ST)
egen group_time = group(group date)

drop if mf_share ==.
drop if maturity_year <0 | maturity_year==.

replace yield=yield*100 // change to bps
sum yield, d
winsor2 yield, cut(5 95) replace
g one = 1
*reghdfe yield , ab( FE2=rating_time )
reghdfe yield , ab( FE2=group_time )
predict u, res

g rank_category=.

foreach i of numlist 0/1 {
foreach j of numlist 0/1 {
sum rank_index_adj if IG==`i' & ST==`j', d
replace rank_category = 1 if rank_index_adj<`r(p25)'& IG==`i' & ST==`j'
replace rank_category = 2 if rank_index_adj>=`r(p25)' & rank_index_adj<`r(p50)'& IG==`i' & ST==`j'
replace rank_category = 3 if rank_index_adj>=`r(p50)' & rank_index_adj<`r(p75)'& IG==`i' & ST==`j'
replace rank_category = 4 if rank_index_adj>=`r(p75)'& rank_index_adj<1& IG==`i' & ST==`j'
}
}
drop if missing(rank_category)
collapse (median) yld_pt=u , by(group IG ST rank_category date)
reshape wide yld_pt,i(group IG ST date) j(rank)

tsset group date

bysort group: g diff = (yld_pt4 - yld_pt1) - (yld_pt4[1] - yld_pt1[1])

twoway (tsline diff if IG==1 & ST==1) ///
(tsline diff if IG==1 & ST==0, lpattern("--")) ///
(tsline diff if IG==0 & ST==1, lpattern(".") ) ///
(tsline diff if IG==0 & ST==0, lpattern("-.")), ///
legend(order(1 2 3 4) label(1 "IG-ST") label(2 "IG-LT") label(3 "HY-ST") label(4 "HY-LT") )  ylabel(, nogrid)  title("")  ///
graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
xtitle("") ytitle("Yield (bps)")  xlabel(,format(%tdm)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\cbond_yield_by_rank_rating_maturity.pdf", replace


bysort group: g march23 = diff-diff[_n-1] if date==td(23mar2020)
bysort group: g april9 = diff-diff[_n-1] if date==td(09apr2020)
sum march23 april9 if IG==1 & ST==1
sum march23 april9 if IG==1 & ST==0
sum march23 april9 if IG==0 & ST==1
sum march23 april9 if IG==0 & ST==0



*********************************************************
* overall yield 
use $\folder\data\return_security_day_level, clear

keep if date>=td(1feb2020) & date<=td(30apr2020)
keep if treasury==0
keep if rating_num>=-10
g IG = (rating_num>=1 & rating_num!=.)
g ST = (maturity_year<5)
egen group = group(IG ST)
egen group_time = group(group date)

replace yield=yield*100
collapse (median) yield , by(group IG ST  date)
twoway (tsline yield if IG==1 & ST==1) ///
(tsline yield if IG==1 & ST==0, lpattern("--")) ///
(tsline yield if IG==0 & ST==1, lpattern(".") ) ///
(tsline yield if IG==0 & ST==0, lpattern("-.")), ///
legend(order(1 2 3 4) label(1 "IG-ST") label(2 "IG-LT") label(3 "HY-ST") label(4 "HY-LT") )  ylabel(, nogrid)  title("")  ///
graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
xtitle("") ytitle("Yield (bps)")  xlabel(,format(%tdm)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\cbond_overall_yield.pdf", replace

*save Figure_cbond_overall_yield_raw_data, replace


*********************************************************
* Summary stats
use $\folder\data\return_security_day_level, clear
drop tm
g tm = mofd(date)
format tm %tm
merge m:1 cusip tm using "liquidity_2020"
drop if _m==2
drop _m

winsor2 yield, cut(5 95) replace
*replace return_win=return_win*100 // change to bps
*replace yield=yield*100 // change to bps

replace mf_share=mf_share*100 // change to percent

keep if date>=td(1jan2020) & date<=td(30apr2020)
label var return_win "Return"
label var outflow "Outflows"
label var liquidation "Liquidation-adjusted outflow"
label var outflow_etf "ETF outflows"
label var yield "Yield"
label var mf_share "Mutual fund ownership"
label var zero_trading "Zero-trading days"


estpost summarize   return_win yield zero_trading outflow liquidation outflow_etf  mf_share ,  d
est store sum_stat
esttab sum_stat using "$folder\output\summary_stats_security.tex", replace ///
cells("mean(fmt(%9.3f)) sd(fmt(%9.3f))  p10(fmt(%9.3f)) p25(fmt(%9.3f)) p50(fmt(%9.3f)) p75(fmt(%9.3f)) p90(fmt(%9.3f)) ") ///
label booktabs nonum gaps noobs ///
collabels( "mean" "sd" "p10" "p25" "p50" "p75" "p90") width(\hsize) 
